package cn.gqr.config;

import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableResourceServer;
import org.springframework.security.oauth2.config.annotation.web.configuration.ResourceServerConfigurerAdapter;
import org.springframework.security.oauth2.config.annotation.web.configurers.ResourceServerSecurityConfigurer;

@Configuration
@EnableResourceServer   //启动资源服务器
@Order(31)
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class OAuth2ResourceServerConfig extends ResourceServerConfigurerAdapter {


    @Override
    public void configure(ResourceServerSecurityConfigurer resources) throws Exception {
        System.out.println("resources");
        resources.stateless(true); //无状态储存
    }

    @Override
    public void configure(HttpSecurity http) throws Exception {
        System.out.println("HttpSecurity");
        http.csrf().disable();
        http
            .authorizeRequests()
            .antMatchers("/api/login/**").permitAll()
            .antMatchers("/api/public/**").permitAll()
            //其他任何请求需要登录
            .anyRequest().authenticated();

    }

    @Bean
    public MybatisPlusInterceptor setInterceptor(){
        MybatisPlusInterceptor interceptor=new MybatisPlusInterceptor();
        interceptor.addInnerInterceptor(new PaginationInnerInterceptor());
        return interceptor;
    }

    @Bean
    public PasswordEncoder passwrodEncoder(){
        return  new BCryptPasswordEncoder();
    }

}
